<?php

namespace api\modules\v1\controllers\system\base;

use Yii;
use api\controllers\OnAuthController;
use common\models\base\BbBodyParts;
use common\helpers\CommonHelper;
use services\base\BbBodyPartsService;
use api\modules\v1\forms\base\BbBodyPartsForm;

class BbBodyPartsController extends OnAuthController
{
    /**
     * @var BbBodyParts
     */
    public $modelClass = BbBodyParts::class;

    /**
     * @api {get} /v1/system/base/bb-body-parts/list 白白部位列表
     * @apiName list
     * @apiGroup BbBodyParts
     *
     * @apiParam {Number} page                                  页码
     * @apiParam {Number} pageSize                              每页显示数量
     *
     * @apiSuccess {Object}  data                               返回数据
     * @apiSuccess {Number}  data.page                          当前页码
     * @apiSuccess {Number}  data.pageSize                      每页显示数
     * @apiSuccess {Number}  data.total                         总数
     * @apiSuccess {Array}   data.list                          患者列表
     * @apiSuccess {Number}  data.list.id                       部位ID
     * @apiSuccess {Number}  data.list.pid                      父部位ID
     * @apiSuccess {String}  data.list.body_parts_name          名称
     * @apiSuccess {Number}  data.list.sort                     排序
     * @apiSuccess {Number}  data.list.status                   状态
     * @apiSuccess {String}  data.list.status_str               状态值
     * @apiSuccess {Array}   data.list.children                 子部位
     *
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        // 查询结果
        $bbBodyPartsService = new BbBodyPartsService();
        return $bbBodyPartsService->getListPage($params, (int)$page, (int)$pageSize);
    }

    /**
     * @api {get} /v1/system/base/bb-body-parts/info 白白部位详情
     * @apiName info
     * @apiGroup BbBodyParts
     *
     * @apiParam {Number} id                                部位ID
     *
     * @apiSuccess {Object}  data                           返回数据
     * @apiSuccess {Number}  data.id                        部位ID
     * @apiSuccess {Number}  data.pid                       父部位ID
     * @apiSuccess {String}  data.body_parts_name           名称
     * @apiSuccess {Number}  data.sort                      排序
     * @apiSuccess {Number}  data.status                    状态
     * @apiSuccess {Array}   data.body_list                 父部位列表
     *
     */
    public function actionInfo()
    {
        $params = Yii::$app->request->get();
        $params['id'] = $params['id'] ?? 0;

        // 查询结果
        $bbBodyPartsService = new BbBodyPartsService();
        return $bbBodyPartsService->getInfo($params);
    }

    /**
     * @api {post} /v1/system/base/bb-body-parts/create 新增部位
     * @apiName create
     * @apiGroup BbBodyParts
     *
     * @apiParam {String} body_parts_name   部位名称
     * @apiParam {Number} pid               父部位ID
     * @apiParam {Number} sort              排序
     *
     * @apiSuccess {Object}  data           返回数据
     * @apiSuccess {Number}  data.id        部位ID
     */
    public function actionCreateBbBodyParts()
    {
        $params = Yii::$app->request->post();
        $params['body_parts_name'] = $params['body_parts_name'] ?? '';
        $params['pid'] = intval($params['pid'] ?? 0);
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new BbBodyPartsForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $bbBodyPartsService = new BbBodyPartsService();
        return $bbBodyPartsService->create($model);
    }

    /**
     * @api {post} /v1/system/base/bb-body-parts/update 新增部位
     * @apiName update
     * @apiGroup BbBodyParts
     *
     * @apiParam {Number} id                部位ID
     * @apiParam {String} body_parts_name   部位名称
     * @apiParam {Number} pid               父部位ID
     * @apiParam {Number} sort              排序
     * @apiParam {Number} status            状态【1启用、0停用】
     *
     * @apiSuccess {Object}  data           返回数据
     * @apiSuccess {Number}  data.id        部位ID
     */
    public function actionUpdateBbBodyParts()
    {
        $params = Yii::$app->request->post();
        $params['body_parts_name'] = $params['body_parts_name'] ?? '';
        $params['pid'] = intval($params['pid'] ?? 0);
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new BbBodyPartsForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $bbBodyPartsService = new BbBodyPartsService();
        return $bbBodyPartsService->update($model);
    }

    /**
     * @api {post} /v1/system/base/bb-body-parts/update-status 编辑部位状态
     * @apiName update-status
     * @apiGroup BbBodyParts
     *
     * @apiParam {Number} id                部位ID
     * @apiParam {Number} status            状态【1启用、0停用】
     *
     * @apiSuccess {Object}  data           返回数据
     * @apiSuccess {Number}  data.id        部位ID
     */
    public function actionUpdateBbBodyPartsStatus()
    {
        $params = Yii::$app->request->post();
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new BbBodyPartsForm();
        $model->setScenario('edit_status');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $bbBodyPartsService = new BbBodyPartsService();
        return $bbBodyPartsService->updateStatus($model);
    }

    public function actionTrueDelete()
    {
        $params = Yii::$app->request->post();
        $id = $params['id'];
        $bbBodyPartsService = new BbBodyPartsService();

        return $bbBodyPartsService->trueDelete($id);
    }

}
